package com.dit.dao;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;



@Repository("genericDao")
public abstract class GenericDao<T> extends BaseDao {

	public static final int MIN_INDEX = 0;
	private Class<T> persistentClass;

	@SuppressWarnings("unchecked")
	public GenericDao(){
	   this.persistentClass = (Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	public List<T> findAll(Class<T> clazz) throws DataAccessException {
		return getHibernateTemplate().loadAll(clazz);
	}

	public T findById(String id) {
		try{
		  return (T)getHibernateTemplate().load(getPersistentClass(), id);
		}catch (Exception e) {
		  return null;
		}
	}
	
	public Class<T> getPersistentClass() {
		return persistentClass;
	}

	public void setPersistentClass(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}




}
